package cn.accjiyun.gomoku.view;

import cn.accjiyun.gomoku.MainApp;
import cn.accjiyun.gomoku.model.DialogType;
import cn.accjiyun.gomoku.model.EngineLevel;
import cn.accjiyun.gomoku.util.Logger;
import cn.accjiyun.gomoku.view.resources.images.MyImageRes;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * Created by jiyun on 2016/9/11.
 */
public class DialogController {
    // Reference to the main application
    private static MainApp MAINAPP;
    private static DialogController dialogController;
    private static Stage stage;
    private static Scene scene;
    private static BorderPane borderPane;

    private static int level = EngineLevel.EASY;

    @FXML
    TextField textField;

    @FXML
    RadioButton radioButtonFool;
    @FXML
    RadioButton radioButtonEasy;
    @FXML
    RadioButton radioButtonNormal;
    @FXML
    RadioButton radioButtonDifficult;
    @FXML
    RadioButton radioButtonCustom;
    ToggleGroup group = new ToggleGroup();
    @FXML
    TextField textFieldDepth;
    @FXML
    TextField textFieldWidth;

    public static DialogController getDialogController() {
        return dialogController;
    }

    public static int getLevel() {
        return level;
    }

    public static void setLevel(int level) {
        DialogController.level = level;
    }

    public static void show(Stage owner, String type, MainApp MAINAPP) {
        try {
            DialogController.MAINAPP = MAINAPP;
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader(MainApp.class
                    .getResource(type));
            borderPane = loader.load();
            dialogController = loader.getController();
            // Show the scene containing the root layout.
            scene = new Scene(borderPane);
            stage = new Stage();
            stage.getIcons().add(MyImageRes.GOMOKUIMAGE);
            stage.setScene(scene);
            stage.initOwner(owner);
            setDialogTitle(type);
            stage.show();
        } catch (IOException e) {
            Logger.exceptionLog("Fail to load Dialog<XX>.fxml", e);
        }
    }

    private static void setDialogTitle(String type) {

        if (type.equals(DialogType.CONFIRM )) {
            stage.setTitle("About");
        } else if (type.equals(DialogType.EDITSIZE)) {
            stage.setTitle("设置棋盘大小");
        } else if (type.equals(DialogType.EDITLEVEL)) {
            stage.setTitle("设置棋力");
        }
    }

    public void initEditSizeDialog() {
        textField.setText(String.valueOf(MAINAPP.getChessboard().getBoardSize()));
    }

    public void initEditLevelDialog() {
        radioButtonFool.setToggleGroup(group);
        radioButtonEasy.setToggleGroup(group);
        radioButtonNormal.setToggleGroup(group);
        radioButtonDifficult.setToggleGroup(group);
        radioButtonCustom.setToggleGroup(group);
        switch (level) {
            case EngineLevel.FOOL : radioButtonFool.setSelected(true); break;
            case EngineLevel.EASY : radioButtonEasy.setSelected(true); break;
            case EngineLevel.NORMAL : radioButtonNormal.setSelected(true); break;
            case EngineLevel.DIFFICULT : radioButtonDifficult.setSelected(true); break;
            case EngineLevel.CUSTOM :
                radioButtonCustom.setSelected(true);
                textFieldDepth.setText(String.valueOf(EngineLevel.saveDepth));
                textFieldWidth.setText(String.valueOf(EngineLevel.saveWidth));
                break;
        }
        borderPane.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (radioButtonCustom.isSelected()) {
                    textFieldDepth.setDisable(false);
                    textFieldWidth.setDisable(false);
                } else {
                    textFieldDepth.setDisable(true);
                    textFieldWidth.setDisable(true);
                }
            }
        });
    }

    @FXML
    public void handleSetLevel() {
        BoardLayoutController controller = MAINAPP.getBoardLayoutController();
        if (radioButtonFool.isSelected()) {
            level = EngineLevel.FOOL;
        } else if (radioButtonEasy.isSelected()) {
            level = EngineLevel.EASY;
        } else if (radioButtonNormal.isSelected()) {
            level = EngineLevel.NORMAL;
        } else if (radioButtonDifficult.isSelected()) {
            level = EngineLevel.DIFFICULT;
        } else if (radioButtonCustom.isSelected()) {
            try {
                int depth = Integer.valueOf(textFieldDepth.getText());
                int width = Integer.valueOf(textFieldWidth.getText());
                if (depth > 0 && width > 0) {
                    EngineLevel.setLevel(EngineLevel.CUSTOM, depth, width);
                    level = EngineLevel.CUSTOM;
                } else {
                    return;
                }
            } catch (Exception e) {
                return;
            }
        }
        EngineLevel.setLevel(level);
        stage.close();
    }

    @FXML
    public void handleEditAction() {
        int boardSize = Integer.valueOf(textField.getText());
        if (boardSize > 21 || boardSize < 15 || boardSize % 2 == 0) {
            return;
        }
        MAINAPP.getChessboard().setBoardSize(boardSize);
        MAINAPP.getChessboard().clear();
        stage.close();
        MAINAPP.getBoardLayoutController().clearBoard();
        reOpenWindow(MAINAPP);
    }

    public static void reOpenWindow(MainApp MAINAPP) {
        DialogController.MAINAPP = MAINAPP;
        MAINAPP.getPrimaryStage().close();
        MAINAPP.getRootLayoutController().adjustStageSize();
        MAINAPP.getBoardLayoutController().drawBoard();
        MAINAPP.getBoardLayoutController().reDrawChess();
        MAINAPP.getPrimaryStage().show();
    }

    @FXML
    public void closeAction() {
         stage.close();
    }
}
